package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.DeviceSampleDao;
import jsyf.oa.dao.system.*;
import jsyf.oa.dao.task.BonusDao;
import jsyf.oa.entity.system.RegionAchivementsEntity;
import jsyf.oa.entity.task.BonusEntity;
import jsyf.oa.facade.system.RegionAchivementsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by y on 2018/8/2.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class RegionAchivementsService implements RegionAchivementsFacade {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RegionAchivementsDao regionAchivementsDao;
    @Autowired
    private RegionDao regionDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private BonusDao bonusDao;
    @Autowired
    private BigAchievementsDao bigAchievementsDao;
    @Autowired
    private DeviceSampleDao deviceSampleDao;

    @Override
    public String initData() {
        //更新区域经理记录的
        //更新区域组长记录(与区域经理相同)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfM = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
        List<JSONObject> regionList = regionAchivementsDao.getRegionManagerList("");
        RegionAchivementsEntity regionAchivementsEntity = new RegionAchivementsEntity();
        for (JSONObject region : regionList) {
            int currentYear = Integer.parseInt(sdfY.format(new Date()));
            int currentMonth = Integer.parseInt(sdf.format(new Date()).split("-")[1]);
            int currentMiddleMonth = 0;
            int currentEndMonth = 0;
            // 2018
            String middleYear = "";
            String endYear = "";
            // 2018-10
            String beginMonth = sdfM.format(new Date());
            String middleMonth = "";
            String endMonth = "";
            //获取入职日期
            String entryDate = region.getString("entryDate");
            int entryDay = Integer.parseInt(entryDate.split("-")[2]);
            if (sdfM.format(new Date()).equals(entryDate.substring(0, 7)) && entryDay > 15) {
                //如果是当月下旬入职，开始月份丛下个月开始算
                currentMonth = (currentMonth + 1) % 12 == 0 ? 12 : (currentMonth + 1) % 12;
                currentYear = (currentMonth == 12) ? currentYear + 1 : currentYear;
            }
            if (check(region.getString("userId"), region.getString("regionId"), currentMonth, currentYear) == null) {
                if (regionAchivementsDao.getBegionMonthByUserId(region.getString("userId")) != null) {
                    //如果之前有这个人的季度，继续保留，否则当前月处理
                    beginMonth = regionAchivementsDao.getBegionMonthByUserId(region.getString("userId")).getString("beginMonth");//2018-10
                    currentYear = Integer.parseInt(beginMonth.substring(0, 4));
                    currentMonth = Integer.parseInt(beginMonth.substring(5, 7));
                }

                if (currentMonth < 10) {
                    beginMonth = String.valueOf(currentYear) + "-" + "0" + String.valueOf(currentMonth);
                } else {
                    beginMonth = String.valueOf(currentYear) + "-" + String.valueOf(currentMonth);
                }

                switch (currentMonth) {
                    case 11:
                        currentMiddleMonth = 12;
                        currentEndMonth = 1;
                        middleYear = String.valueOf(currentYear);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                    case 12:
                        currentMiddleMonth = 1;
                        currentEndMonth = 2;
                        middleYear = String.valueOf(currentYear + 1);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                    default:
                        currentMiddleMonth = currentMonth + 1;
                        currentEndMonth = currentMonth + 2;
                        middleYear = String.valueOf(currentYear);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                }

                if (currentMiddleMonth < 10) {
                    middleMonth = middleYear + "-" + "0" + String.valueOf(currentMiddleMonth);
                } else {
                    middleMonth = middleYear + "-" + String.valueOf(currentMiddleMonth);
                }


                if (currentEndMonth < 10) {
                    endMonth = endYear + "-" + "0" + String.valueOf(currentEndMonth);
                } else {
                    endMonth = endYear + "-" + String.valueOf(currentEndMonth);
                }

                regionAchivementsEntity.setSaleTargetNum(1);
                regionAchivementsEntity.setUserId(region.getString("userId"));
                regionAchivementsEntity.setAreaId(region.getString("regionId"));
                regionAchivementsEntity.setBeginMonth(beginMonth);
                regionAchivementsEntity.setMiddleMonth(middleMonth);
                regionAchivementsEntity.setEndMonth(endMonth);
                regionAchivementsDao.add(regionAchivementsEntity);
            }

        }

        //传入userId 遍历从前的
        String currentMonthForDelete = sdfM.format(new Date());
        for (JSONObject region : regionList) {
            List<JSONObject> regionIds = regionAchivementsDao.getRegionManagerList(" AND t_sys_user.id='" + region.getString("userId") + "'");
            String ids = "";
            StringBuffer sql = new StringBuffer();
            if (regionIds.size() > 0) {
                for (JSONObject object : regionIds) {
                    sql.append("'");
                    sql.append(object.getString("regionId"));
                    sql.append("'");
                    sql.append(",");
                }
                ids = sql.toString().substring(0, sql.toString().length() - 1);
            }
            regionAchivementsDao.removeByCondition(" AND AREA_ID not in(" + ids + ") AND USER_ID=?  AND ? IN(BEGIN_MONTH,MIDDLE_MONTH,END_MONTH)", new String[]{region.getString("userId"), String.valueOf(currentMonthForDelete)});
        }
        return "插入完成";
    }

    @Override
    public String initDataByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        //更新区域经理记录的
        //更新区域组长记录的 -10月14日添加
        SimpleDateFormat sdfM = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
        List<JSONObject> regionList = regionAchivementsDao.getRegionManagerList(" AND USER_ID='" + userId + "'");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        RegionAchivementsEntity regionAchivementsEntity = new RegionAchivementsEntity();

        for (JSONObject region : regionList) {
            int currentYear = Integer.parseInt(sdfY.format(new Date()));
            int currentMonth = Integer.parseInt(sdf.format(new Date()).split("-")[1]);
            int currentMiddleMonth = 0;
            int currentEndMonth = 0;
            // 2018
            String middleYear = "";
            String endYear = "";
            // 2018-10
            String beginMonth = sdfM.format(new Date());
            String middleMonth = "";
            String endMonth = "";
            //获取入职日期
            String entryDate = region.getString("entryDate");
            int entryDay = Integer.parseInt(entryDate.split("-")[2]);
            if (sdfM.format(new Date()).equals(entryDate.substring(0, 7)) && entryDay > 15) {
                //如果是当月下旬入职，开始月份丛下个月开始算
                currentMonth = (currentMonth + 1) % 12 == 0 ? 12 : (currentMonth + 1) % 12;
                currentYear = (currentMonth == 12) ? currentYear + 1 : currentYear;
            }
            if (check(region.getString("userId"), region.getString("regionId"), currentMonth, currentYear) == null) {
                if (regionAchivementsDao.getBegionMonthByUserId(region.getString("userId")) != null) {
                    //如果之前有这个人的季度，继续保留，否则当前月处理
                    beginMonth = regionAchivementsDao.getBegionMonthByUserId(region.getString("userId")).getString("beginMonth");//2018-10
                    currentYear = Integer.parseInt(beginMonth.substring(0, 4));
                    currentMonth = Integer.parseInt(beginMonth.substring(5, 7));
                }

                if (currentMonth < 10) {
                    beginMonth = String.valueOf(currentYear) + "-" + "0" + String.valueOf(currentMonth);
                } else {
                    beginMonth = String.valueOf(currentYear) + "-" + String.valueOf(currentMonth);
                }

                switch (currentMonth) {
                    case 11:
                        currentMiddleMonth = 12;
                        currentEndMonth = 1;
                        middleYear = String.valueOf(currentYear);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                    case 12:
                        currentMiddleMonth = 1;
                        currentEndMonth = 2;
                        middleYear = String.valueOf(currentYear + 1);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                    default:
                        currentMiddleMonth = currentMonth + 1;
                        currentEndMonth = currentMonth + 2;
                        middleYear = String.valueOf(currentYear);
                        endYear = String.valueOf(currentYear + 1);
                        break;
                }

                if (currentMiddleMonth < 10) {
                    middleMonth = middleYear + "-" + "0" + String.valueOf(currentMiddleMonth);
                } else {
                    middleMonth = middleYear + "-" + String.valueOf(currentMiddleMonth);
                }


                if (currentEndMonth < 10) {
                    endMonth = endYear + "-" + "0" + String.valueOf(currentEndMonth);
                } else {
                    endMonth = endYear + "-" + String.valueOf(currentEndMonth);
                }

                regionAchivementsEntity.setSaleTargetNum(1);
                regionAchivementsEntity.setUserId(region.getString("userId"));
                regionAchivementsEntity.setAreaId(region.getString("regionId"));
                regionAchivementsEntity.setBeginMonth(beginMonth);
                regionAchivementsEntity.setMiddleMonth(middleMonth);
                regionAchivementsEntity.setEndMonth(endMonth);
                regionAchivementsDao.add(regionAchivementsEntity);
            }

        }

        //传入userId 遍历从前的
        String currentMonthForDelete = sdfM.format(new Date());
        for (JSONObject region : regionList) {
            List<JSONObject> regionIds = regionAchivementsDao.getRegionManagerList(" AND t_sys_user.id='" + region.getString("userId") + "'");
            String ids = "";
            StringBuffer sql = new StringBuffer();
            if (regionIds.size() > 0) {
                for (JSONObject object : regionIds) {
                    sql.append("'");
                    sql.append(object.getString("regionId"));
                    sql.append("'");
                    sql.append(",");
                }
                ids = sql.toString().substring(0, sql.toString().length() - 1);
            }
            regionAchivementsDao.removeByCondition(" AND AREA_ID not in(" + ids + ") AND USER_ID=? AND ? IN(BEGIN_MONTH,MIDDLE_MONTH,END_MONTH) ", new String[]{region.getString("userId"), String.valueOf(currentMonthForDelete)});
        }
        return "更新成功";
    }

    public JSONObject check(String userId, String bigRegionId, int currentMonth, int currentYear) {
        return regionAchivementsDao.check(userId, bigRegionId, currentMonth, currentYear);
    }

    @Override
    public String initDataByCurrentMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
        int currentMonth = Integer.parseInt(sdf.format(new Date()).split("-")[1]);
        int currentYear = Integer.parseInt(sdfY.format(new Date()));
        int tempMonth = currentMonth - 1;
        int tempYear = currentYear;
        if (tempMonth == 0) {//当前月为1月份
            tempMonth = 12;
            tempYear = currentYear - 1;
        }
        String month = "";
        if (tempMonth < 10) {
            month = String.valueOf(tempYear) + "-0" + String.valueOf(tempMonth);
        } else {
            month = String.valueOf(tempYear) + "-" + String.valueOf(tempMonth);
        }
        //获取到上月 endMonth的记录
        List<JSONObject> regionList = regionAchivementsDao.getRegionMonth(month);
        String begionMonth = sdf.format(new Date());//当前年月
        String milleMonth = "";
        String endMonth = "";
        //获取当前月，往后两个月
        switch (currentMonth) {
            case 12:
                milleMonth=String.valueOf(currentYear+1)+"-01";
                endMonth=String.valueOf(currentYear+1)+"-02";
                break;
            case 11:
                milleMonth=String.valueOf(currentYear)+"-12";
                endMonth=String.valueOf(currentYear+1)+"-01";
                break;
            case 10:
                milleMonth=String.valueOf(currentYear)+"-11";
                endMonth=String.valueOf(currentYear)+"-12";
                break;
            case 9:
                milleMonth=String.valueOf(currentYear)+"-10";
                endMonth=String.valueOf(currentYear)+"-11";
                break;
            case 8:
                milleMonth=String.valueOf(currentYear)+"-09";
                endMonth=String.valueOf(currentYear)+"-10";
                break;
            default:
                milleMonth=String.valueOf(currentYear)+"-0"+String.valueOf(currentMonth+1);
                endMonth=String.valueOf(currentYear)+"-0"+String.valueOf(currentMonth+2);
                break;
        }
        if (regionList.size() > 0) {
            for (JSONObject object : regionList) {
                RegionAchivementsEntity regionAchivementsEntity = new RegionAchivementsEntity();
                regionAchivementsEntity.setUserId(object.getString("userId"));
                regionAchivementsEntity.setAreaId(object.getString("regionId"));
                regionAchivementsEntity.setBeginMonth(begionMonth);
                regionAchivementsEntity.setMiddleMonth(milleMonth);
                regionAchivementsEntity.setEndMonth(endMonth);
                regionAchivementsEntity.setSaleTargetNum(1);
                regionAchivementsDao.add(regionAchivementsEntity);
            }
        }
        return "更新成功";
    }

    @Override
    public JSONObject getManagerSaleList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String roleName = jsonParams.getString("roleName");
        StringBuilder condition = new StringBuilder();
        BigDecimal sumTargetNum = new BigDecimal(0);
        BigDecimal sumFinishNum = new BigDecimal(0);
        BigDecimal sumSalePunishment = new BigDecimal(0);
        BigDecimal sumSaleReward = new BigDecimal(0);
        if (!StringUtils.isBlank(roleName)) {
            if (roleName.equals("大区经理") || roleName.equals("销售管理") || roleName.equals("销售总监") || roleName.equals("区域支持")
                    || roleName.equals("区域经理") || roleName.equals("区域组长") || roleName.equals("区域总监")) {
                condition.append(" AND t_sys_company.ID= '" + companyId + "' ");
            }
        }
        JSONObject result = new JSONObject();
        List<JSONObject> list = regionAchivementsDao.getNewManagerSale(dateTime, condition.toString());
        for (JSONObject object : list) {
            sumTargetNum = sumTargetNum.add(object.getBigDecimal("targetNum"));
            sumFinishNum = sumFinishNum.add(object.getBigDecimal("finishNum"));
            sumSalePunishment = sumSalePunishment.add(object.getBigDecimal("salePunishment"));
            sumSaleReward = sumSaleReward.add(object.getBigDecimal("saleReward"));
        }
        String month = "";
        if (dateTime.substring(5, 7).startsWith("0")) {
            month = dateTime.substring(6);
        } else {
            month = dateTime.substring(5, 7);
        }
        JSONObject total = new JSONObject();
        total.put("companyName", "合计");
        total.put("targetNum", sumTargetNum);
        total.put("finishNum", sumFinishNum);
        total.put("salePunishment", sumSalePunishment);
        total.put("saleReward", sumSaleReward);
        total.put("money", sumSalePunishment.add(sumSaleReward));
        total.put("companyId", companyId);
        total.put("cycle", month);
        total.put("userId", "");
        total.put("userName", "");
        total.put("regionName", "");
        total.put("roleName", " ");

        list.add(total);
        result.put("records", list);
        return result;
    }

    /*
    * 如果发现有区域经理数据没有带出来，看看是否配置区域
    * */
    @Override
    public JSONObject getSaleList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime").substring(0, 7);

        String month = "";
        if (dateTime.substring(5, 7).startsWith("0")) {
            month = dateTime.substring(6);
        } else {
            month = dateTime.substring(5, 7);
        }
        List<JSONObject> list = regionAchivementsDao.getNewSale(companyId, dateTime, month);//销售总监，区域总监
        List<JSONObject> listBigManagerList = regionAchivementsDao.getNewSaleBigManagerList(companyId, dateTime, month);//大区经理
        List<JSONObject> listSmallManagerList = regionAchivementsDao.getNewSaleSmallManagerList(companyId, dateTime, month);//区域组长
        List<JSONObject> listManagerList = regionAchivementsDao.getNewSaleManagerList(companyId, dateTime, month);//区域经理
        BigDecimal sumTargetNum = new BigDecimal(0);
        BigDecimal sumFinishNum = new BigDecimal(0);
        BigDecimal sumSalePunishment = new BigDecimal(0);
        BigDecimal sumSaleReward = new BigDecimal(0);
        for (JSONObject object : list) {
            sumTargetNum = sumTargetNum.add(object.getBigDecimal("targetNum"));
            sumFinishNum = sumFinishNum.add(object.getBigDecimal("finishNum"));
            sumSalePunishment = sumSalePunishment.add(object.getBigDecimal("salePunishment"));
            sumSaleReward = sumSaleReward.add(object.getBigDecimal("saleReward"));
        }
        for (JSONObject object : listBigManagerList) {
            sumTargetNum = sumTargetNum.add(object.getBigDecimal("targetNum"));
            sumFinishNum = sumFinishNum.add(object.getBigDecimal("finishNum"));
            sumSalePunishment = sumSalePunishment.add(object.getBigDecimal("salePunishment"));
            sumSaleReward = sumSaleReward.add(object.getBigDecimal("saleReward"));
        }
        for (JSONObject object : listSmallManagerList) {
            sumTargetNum = sumTargetNum.add(object.getBigDecimal("targetNum"));
            sumFinishNum = sumFinishNum.add(object.getBigDecimal("finishNum"));
            sumSalePunishment = sumSalePunishment.add(object.getBigDecimal("salePunishment"));
            sumSaleReward = sumSaleReward.add(object.getBigDecimal("saleReward"));
        }
        for (JSONObject object : listManagerList) {
            sumTargetNum = sumTargetNum.add(object.getBigDecimal("targetNum"));
            sumFinishNum = sumFinishNum.add(object.getBigDecimal("finishNum"));
            sumSalePunishment = sumSalePunishment.add(object.getBigDecimal("salePunishment"));
            sumSaleReward = sumSaleReward.add(object.getBigDecimal("saleReward"));
        }
        JSONObject total = new JSONObject();
        total.put("companyName", "合计");
        total.put("targetNum", sumTargetNum);
        total.put("finishNum", sumFinishNum);
        total.put("salePunishment", sumSalePunishment);
        total.put("saleReward", sumSaleReward);
        total.put("money", sumSalePunishment.add(sumSaleReward));
        total.put("companyId", companyId);
        total.put("cycle", month);
        total.put("userId", "");
        total.put("userName", "");
        total.put("regionName", "");
        total.put("roleName", " ");

        listManagerList.add(total);
        list.addAll(listBigManagerList);
        list.addAll(listSmallManagerList);
        list.addAll(listManagerList);
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;
    }

    @Override
    public List<JSONObject> getRegionMonthList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int currentMonth = Integer.parseInt(sdf.format(new Date()).split("-")[1]);
        int currentYear = Integer.parseInt(sdf.format(new Date()).split("-")[0]);
        String month = "";
        if (currentMonth < 10) {
            month = String.valueOf(currentYear) + "-0" + String.valueOf(currentMonth);
        } else {
            month = String.valueOf(currentYear) + "-" + String.valueOf(currentMonth);
        }

        List<JSONObject> regionList = regionAchivementsDao.getRegionMonth(month);
        return regionList;
    }

    @Override
    public List<JSONObject> getRegionDepartId(String id) {
        List<JSONObject> regions = userDao.getViewJSONListByCondition(" AND ID=?", new Object[]{id});
        return regions;
    }

    @Override
    public Boolean add(String id, String departId, String begionMonth, String milleMonth, String endMonth) {
        RegionAchivementsEntity regionAchivementsEntity = new RegionAchivementsEntity();
        regionAchivementsEntity.setUserId(id);
        regionAchivementsEntity.setSaleTargetNum(1);
        regionAchivementsEntity.setAreaId(departId);
        regionAchivementsEntity.setBeginMonth(begionMonth);
        regionAchivementsEntity.setMiddleMonth(milleMonth);
        regionAchivementsEntity.setEndMonth(endMonth);
        regionAchivementsDao.add(regionAchivementsEntity);
        return true;
    }

    public List<JSONObject> getRegionByUserId(List<JSONObject> list) {
        for (int i = 0; i < list.size(); i++) {
            if ("区域经理".equals(list.get(i).getString("roleName"))) {
                String userId = list.get(i).getString("userId");
                JSONObject region = regionDao.getRegionByRoleName(userId, "AND PARENT_ID != '0'");
                if (region != null) {
                    list.get(i).put("regionName", region.getString("name"));
                }
            }
            if ("大区经理".equals(list.get(i).getString("roleName"))) {
                String userId = list.get(i).getString("userId");
                JSONObject region = regionDao.getBigRegionByRoleName(userId, "AND t_sys_user.JOB_STATE != 1");
                if (region != null) {
                    list.get(i).put("regionName", region.getString("name"));
                }
            }
        }
        return list;
    }

    @Override
    public List<JSONObject> getCountRegionMonthList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String currentMonth=sdf.format(new Date());
        List<JSONObject> regionList = regionAchivementsDao.getCountRegionMonthList(currentMonth);
        return regionList;
    }

    @Override
    public List<JSONObject> getDeviceType(String userId) {
        return regionAchivementsDao.getDeviceType(userId);
    }

    @Override
    public JSONObject getBigRegionDeviceType(String userId) {
        return regionAchivementsDao.getBigRegionDeviceType(userId);
    }

    @Override
    public List<JSONObject> getBigRegionList(String userId, String companyId, String roleNames, String condition) {
        return regionAchivementsDao.getBigRegionList(userId, companyId, roleNames, condition);
    }

    @Override
    public List<JSONObject> getAllCheckUser(String companyId) {
        return regionAchivementsDao.getAllCheckUser(" AND v_sys_part_time_for_report.companyId='" + companyId + "'  GROUP BY userId");
    }

    @Override
    public List<JSONObject> getPartByUserId(String userId, String companyId) {
        return regionAchivementsDao.getAllCheckUser(" AND userId='" + userId + "' and v_sys_part_time_for_report.companyId='" + companyId + "' GROUP BY userId,t_sys_role_name.`NAME`,t_sys_depart.ID");
    }

    @Override
    public JSONObject getTargetByUserId(String userId, String regionId) {
        return regionAchivementsDao.getTargetByUserId(userId, regionId);
    }

    @Override
    public JSONObject getBigRegionIdByUserIdAndCompanyId(String userId, String departName) {
        return regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userId, departName);
    }

    @Override
    public List<JSONObject> getCompanyList() {
        return regionAchivementsDao.getCompanyList();
    }

    @Override
    public List<JSONObject> getBigRegionByUserId(String userId, String companyId) {
        return regionAchivementsDao.getBigRegionByUserId(userId, companyId);
    }

    /*生成在职人员绩效数据。有问题点一下
    * */
    @Override
    public String countCurrentMoney() {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        String month = sdf1.format(new Date());
        bonusDao.removeByCondition(" AND TYPE=1 AND JOB_STATE=0 AND left(DAY,7) = LEFT (NOW(), 7)", new String[]{});
        List<JSONObject> companyList = companyDao.getJSONListByCondition(" AND DEL_FLAG=0", null);
        for (int i = 0; i < companyList.size(); i++) {
            //从人开始遍历
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String today = sdf.format(new Date());
            List<JSONObject> users = regionAchivementsDao.getAllCheckUser(" AND v_sys_part_time_for_report.companyId='" + companyList.get(i).getString("id") + "' AND v_sys_part_time_for_report.userId NOT IN (\n" +
                    "\tSELECT\n" +
                    "\t\tt_sys_user.id\n" +
                    "\tFROM\n" +
                    "\t\tt_sys_user\n" +
                    "\tWHERE\n" +
                    "\t\tLEFT (t_sys_user.ENTRY_DATE, 7) = '" + month + "' AND RIGHT(t_sys_user.ENTRY_DATE, 2)>15\n" +
                    ") GROUP BY userId");
            for (JSONObject user : users) {
                JSONObject jlJsonObject = new JSONObject();
                jlJsonObject.put("money", "0");
                jlJsonObject.put("remark", "");
                JSONObject cfJsonObject = new JSONObject();
                cfJsonObject.put("money", "0");
                cfJsonObject.put("remark", "");
                JSONObject newJsonoObject = new JSONObject();
                newJsonoObject.put("money", "0");
                newJsonoObject.put("remark", "");
                if (!StringUtils.isBlank(user.getString("userId"))) {
                    // 获取所有的兼职情况
                    List<JSONObject> userParts = regionAchivementsDao.getAllCheckUser(" AND userId='" + user.getString("userId") + "' and v_sys_part_time_for_report.companyId='" + user.getString("companyId") + "' GROUP BY userId,t_sys_role_name.`NAME`,t_sys_depart.ID");
                    for (JSONObject userPart : userParts) {
                        BonusEntity bonusEntity = new BonusEntity();
                        bonusEntity.setUserId(user.getString("userId"));
                        bonusEntity.setType(1);
                        bonusEntity.setJobState(0);
                        bonusEntity.setDay(today);
                        bonusEntity.setRoleName(userPart.getString("roleName"));
                        bonusEntity.setBigRegionId(userPart.getString("regionId"));
                        bonusEntity.setCompanyId(userPart.getString("companyId"));
                        String roleName = userPart.getString("roleName");
                        String userId = userPart.getString("userId");
                        String userDepartName = userPart.getString("departName");
                        String userCompanyId = userPart.getString("companyId");
                        String userDepartId = userPart.getString("departId");
                        String userRegiond = userPart.getString("regionId");
                        //区域经理入职三个月内，不算绩效罚款
                        String entryDate=userPart.getString("entryDate");//入职日期
                        Date dt= null;
                        try {
                            dt = sdf1.parse(entryDate);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        rightNow.add(Calendar.MONTH,2);//日期加2个月
                        Date dt1=rightNow.getTime();
                        String reStr = sdf1.format(dt1);

                        if ("区域经理".equals(roleName) || "区域组长".equals(roleName)) {
                            String regionId = "";
                            if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userId, userDepartName) != null) {
                                regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userId, userDepartName).getString("regionId");
                            } else {
                                continue;
                            }
                            JSONObject num = getRegionDeviceType(userId, regionId);
                            BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                            BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                            BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                            BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                            BigDecimal targetNum = new BigDecimal(num.getString("targetNum"));
                            Double money = 0.00;
                            if (sumNum.subtract(targetNum).doubleValue() >= 0) {
                                money = 1000 * smallNum.doubleValue() + 2000 * middleNum.doubleValue() + 3000 * bigNum.doubleValue();
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userDepartName + "卖出" + sumNum + "台,应奖励" + money + "元");
                            } else if (sumNum.subtract(targetNum).doubleValue()<0  && month.compareTo(reStr)>0){
                                money = 1000 * (sumNum.subtract(targetNum)).doubleValue();
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userDepartName + "未完成" + (targetNum.subtract(sumNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(targetNum);
                            bonusEntity.setSaleFinishNum(sumNum);
                        } else if ("大区经理".equals(roleName)) {
                            List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userId, userCompanyId, "'区域经理','区域组长'", "AND t_sys_depart.ID='" + userDepartId + "'");
                            String regionId = "";
                            if (bigAchievementsDao.getBigRegionTargetBycompanyId(userDepartName, userCompanyId) != null) {
                                regionId = bigAchievementsDao.getBigRegionTargetBycompanyId(userDepartName, userCompanyId).getString("id");
                            } else {
                                continue;
                            }
                            JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "'\n AND MONTH='" + month + "'");
                            BigDecimal targetNum = new BigDecimal(0);
                            BigDecimal finishNum = new BigDecimal(0);
                            BigDecimal finishsmallNum = new BigDecimal(0);
                            BigDecimal finishmiddleNum = new BigDecimal(0);
                            BigDecimal finishBigNum = new BigDecimal(0);
                            Double money = 0.00;
                            if (bigTarget != null) {
                                if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                    targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                                }
                            }
                            for (JSONObject region : regions) {
                                JSONObject num = getRegionFinish(region.getString("userId"), userRegiond, roleName, userId);
                                BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                                BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                                BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                                BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                                finishsmallNum = finishsmallNum.add(smallNum);
                                finishmiddleNum = finishmiddleNum.add(middleNum);
                                finishBigNum = finishBigNum.add(bigNum);
                                finishNum = finishNum.add(sumNum);
                            }
                            //大区经理自己的
                            JSONObject self = getRegionFinish(userId, regionId, roleName, userId);
                            finishsmallNum = finishsmallNum.add(new BigDecimal(self.getString("smallNum")));
                            finishmiddleNum = finishmiddleNum.add(new BigDecimal(self.getString("middleNum")));
                            finishBigNum = finishBigNum.add(new BigDecimal(self.getString("bigNum")));
                            finishNum = finishNum.add(new BigDecimal(self.getString("sumNum")));


                            if (finishNum.subtract(targetNum).doubleValue() >= 0) {
                                money = 1000 * finishsmallNum.doubleValue() + 2000 * finishmiddleNum.doubleValue() + 3000 * finishBigNum.doubleValue();
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userDepartName + "卖出" + finishNum + "台,应奖励" + money + "元");
                            } else {
                                money = 3000 * (finishNum.subtract(targetNum)).doubleValue();
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userDepartName + "未完成" + (targetNum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(targetNum);
                            bonusEntity.setSaleFinishNum(finishNum);
                        } else {//销售总监，总经理
                            //唐玉丰兼职特殊处理
                            if ("20180426083647FCBC7D08C7B14E496E".equals(companyList.get(i).getString("id")) && "2017121620304867AB2903435C97759D".equals(userId)) {
                                break;
                            }
                            if ("2018042608364206B46EC406F62EC079".equals(companyList.get(i).getString("id")) && "2017121620304867AB2903435C97759D".equals(userId)) {
                                break;
                            }

                            List<JSONObject> bigRegions = regionAchivementsDao.getBigRegionByUserId(userId, userCompanyId);
                            BigDecimal bigTargetSum = new BigDecimal(0);
                            for (JSONObject bigRegion : bigRegions) {
                                BigDecimal targetNum = new BigDecimal(0);
                                String regionId = bigRegion.getString("regionId");
                                JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "' AND MONTH='" + month + "'\n");
                                if (bigTarget != null) {
                                    if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                        targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                                    }
                                }
                                bigTargetSum = bigTargetSum.add(targetNum);
                            }
                            List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userId, userCompanyId, "'区域经理','区域组长'", "");
                            BigDecimal targetNum = new BigDecimal(0);
                            BigDecimal finishNum = new BigDecimal(0);
                            BigDecimal finishsmallNum = new BigDecimal(0);
                            BigDecimal finishmiddleNum = new BigDecimal(0);
                            BigDecimal finishBigNum = new BigDecimal(0);
                            Double money = 0.00;
                            for (JSONObject region : regions) {
                                String regionId = "";
                                if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")) != null) {
                                    regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")).getString("regionId");
                                } else {
                                    continue;
                                }
                                JSONObject num = getRegionFinish(region.getString("userId"), regionId, roleName, userId);
                                BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                                BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                                BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                                BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                                finishsmallNum = finishsmallNum.add(smallNum);
                                finishmiddleNum = finishmiddleNum.add(middleNum);
                                finishBigNum = finishBigNum.add(bigNum);
                                finishNum = finishNum.add(sumNum);
                            }
                            if (finishNum.subtract(bigTargetSum).doubleValue() >= 0) {
                                money = 150 * finishsmallNum.doubleValue() + 300 * finishmiddleNum.doubleValue() + 500 * finishBigNum.doubleValue();
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userDepartName + "卖出" + finishNum + "台,应奖励" + money + "元");

                            } else {
                                money = 500 * (finishNum.subtract(bigTargetSum)).doubleValue();
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userDepartName + "未完成" + (bigTargetSum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(bigTargetSum);
                            bonusEntity.setSaleFinishNum(finishNum);
                        }
                        bonusDao.add(bonusEntity);
                    }
                }

            }

        }
        return "更新成功";
    }

    /*生成离职人员绩效数据，传入上月month，生成上月离职人员数据
* */
    @Override
    public String initLzData(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String month = jsonParams.getString("month");
        bonusDao.removeByCondition(" AND TYPE=1  AND JOB_STATE=1 AND left(DAY,7) = '" + month + "'", new String[]{});
        String today = month + "-30";
        List<JSONObject> companyList = companyDao.getJSONListByCondition(" AND DEL_FLAG=0", null);
        for (int i = 0; i < companyList.size(); i++) {
            //从人开始遍历
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<JSONObject> users = regionAchivementsDao.getLzUsers(month);
            for (JSONObject user : users) {
                if (user.getString("userId").equals("20180521150245C3A1D4E6C2CE1085CE")) {
                    System.out.println("1231+++");
                }
                System.out.println(user.getString("userId"));
                JSONObject jlJsonObject = new JSONObject();
                jlJsonObject.put("money", "0");
                jlJsonObject.put("remark", "");
                JSONObject cfJsonObject = new JSONObject();
                cfJsonObject.put("money", "0");
                cfJsonObject.put("remark", "");
                JSONObject newJsonoObject = new JSONObject();
                newJsonoObject.put("money", "0");
                newJsonoObject.put("remark", "");
                if (!StringUtils.isBlank(user.getString("userId"))) {
                    // 获取所有的兼职情况
                    List<JSONObject> userParts = regionAchivementsDao.getAllCheckUser2(" AND userId='" + user.getString("userId") + "' and v_sys_part_time_for_report.companyId='" + companyList.get(i).getString("id") + "' GROUP BY userId,t_sys_role_name.`NAME`,t_sys_depart.ID");

                    for (JSONObject userPart : userParts) {
                        BonusEntity bonusEntity = new BonusEntity();
                        bonusEntity.setUserId(user.getString("userId"));
                        bonusEntity.setType(1);
                        bonusEntity.setDay(today);
                        bonusEntity.setJobState(1);
                        bonusEntity.setRoleName(userPart.getString("roleName"));
                        bonusEntity.setBigRegionId(userPart.getString("regionId"));
                        bonusEntity.setCompanyId(userPart.getString("companyId"));
                        String roleName = userPart.getString("roleName");
                        //if ("区域经理".equals(roleName)) {
                        if ("区域经理".equals(roleName) || "区域组长".equals(roleName)) {
                            String regionId = "";
                            if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")) != null) {
                                regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")).getString("regionId");
                            } else {
                                continue;
                            }
                            JSONObject num = getRegionDeviceType(userPart.getString("userId"), regionId);
                            BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                            BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                            BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                            BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                            BigDecimal targetNum = new BigDecimal(num.getString("targetNum"));
                            Double money = 0.00;
                            if (sumNum.subtract(targetNum).doubleValue() >= 0) {

                                money = 1000 * smallNum.doubleValue() + 2000 * middleNum.doubleValue() + 3000 * bigNum.doubleValue();
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userPart.getString("departName") + "卖出" + sumNum + "台,应奖励" + money + "元");
                            } else {
                                money = 1000 * (sumNum.subtract(targetNum)).doubleValue();
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(sumNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(targetNum);
                            bonusEntity.setSaleFinishNum(sumNum);
                        }
                        else if ("大区经理".equals(roleName)) {
                            List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "AND t_sys_depart.ID='" + userPart.getString("departId") + "'");
                            String regionId = "";
                            if (bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")) != null) {
                                regionId = bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")).getString("id");
                                ;
                            } else {
                                continue;
                            }
                            JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "'\n AND MONTH='" + month + "'");
                            BigDecimal targetNum = new BigDecimal(0);
                            BigDecimal finishNum = new BigDecimal(0);
                            BigDecimal finishsmallNum = new BigDecimal(0);
                            BigDecimal finishmiddleNum = new BigDecimal(0);
                            BigDecimal finishBigNum = new BigDecimal(0);
                            Double money = 0.00;
                            if (bigTarget != null) {
                                if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                    targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                                }
                            }
                            for (JSONObject region : regions) {
                                JSONObject num = getRegionFinish(region.getString("userId"), userPart.getString("regionId"), roleName, userPart.getString("userId"));
                                BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                                BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                                BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                                BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                                finishsmallNum = finishsmallNum.add(smallNum);
                                finishmiddleNum = finishmiddleNum.add(middleNum);
                                finishBigNum = finishBigNum.add(bigNum);
                                finishNum = finishNum.add(sumNum);
                            }
                            //大区经理自己的
                            JSONObject self = getRegionFinish(userPart.getString("userId"), regionId, roleName, userPart.getString("userId"));
                            finishsmallNum = finishsmallNum.add(new BigDecimal(self.getString("smallNum")));
                            finishmiddleNum = finishmiddleNum.add(new BigDecimal(self.getString("middleNum")));
                            finishBigNum = finishBigNum.add(new BigDecimal(self.getString("bigNum")));
                            finishNum = finishNum.add(new BigDecimal(self.getString("sumNum")));


                            if (finishNum.subtract(targetNum).doubleValue() >= 0) {
                                money = 1000 * finishsmallNum.doubleValue() + 2000 * finishmiddleNum.doubleValue() + 3000 * finishBigNum.doubleValue();
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");
                            } else {
                                money = 3000 * (finishNum.subtract(targetNum)).doubleValue();
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(targetNum);
                            bonusEntity.setSaleFinishNum(finishNum);
                        } else {
                            List<JSONObject> bigRegions = regionAchivementsDao.getBigRegionByUserId(userPart.getString("userId"), userPart.getString("companyId"));
                            BigDecimal bigTargetSum = new BigDecimal(0);
                            for (JSONObject bigRegion : bigRegions) {
                                BigDecimal targetNum = new BigDecimal(0);
                                String regionId = bigRegion.getString("regionId");
                                JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "' AND MONTH='" + month + "'\n");
                                if (bigTarget != null) {
                                    if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                        targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                                    }
                                }
                                bigTargetSum = bigTargetSum.add(targetNum);
                            }
                            List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "");
                            BigDecimal targetNum = new BigDecimal(0);
                            BigDecimal finishNum = new BigDecimal(0);
                            BigDecimal finishsmallNum = new BigDecimal(0);
                            BigDecimal finishmiddleNum = new BigDecimal(0);
                            BigDecimal finishBigNum = new BigDecimal(0);
                            Double money = 0.00;
                            for (JSONObject region : regions) {
                                String regionId = "";
                                if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")) != null) {
                                    regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")).getString("regionId");
                                } else {
                                    continue;
                                }
                                JSONObject num = getRegionFinish(region.getString("userId"), regionId, roleName, userPart.getString("userId"));
                                BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                                BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                                BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                                BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                                finishsmallNum = finishsmallNum.add(smallNum);
                                finishmiddleNum = finishmiddleNum.add(middleNum);
                                finishBigNum = finishBigNum.add(bigNum);
                                finishNum = finishNum.add(sumNum);
                            }
                            if (finishNum.subtract(bigTargetSum).doubleValue() >= 0) {
                                money = 150 * finishsmallNum.doubleValue() + 300 * finishmiddleNum.doubleValue() + 500 * finishBigNum.doubleValue();
                                //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+finishNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                                bonusEntity.setSaleReward(new BigDecimal(money));
                                bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");

                            } else {
                                money = 500 * (finishNum.subtract(bigTargetSum)).doubleValue();
                                //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(bigTargetSum.subtract(finishNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                                bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                                bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (bigTargetSum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                            }
                            bonusEntity.setSaleTargetNum(bigTargetSum);
                            bonusEntity.setSaleFinishNum(finishNum);
                        }
                        bonusDao.add(bonusEntity);
                    }
                }
            }

        }
        return "更新成功";
    }

    @Override
    public String transferPosCountMoney(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        String month = sdf1.format(new Date());
        String userId = jsonParams.getString("userId");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        bonusDao.removeByCondition(" AND TYPE=1 AND JOB_STATE=0 AND left(DAY,7) = LEFT (NOW(), 7) AND USER_ID=?", new String[]{userId});
        // 获取所有的兼职情况
        List<JSONObject> userParts = regionAchivementsDao.getAllCheckUser(" AND userId='" + userId + "' GROUP BY userId,t_sys_role_name.`NAME`,t_sys_depart.ID");
        for (JSONObject userPart : userParts) {
            BonusEntity bonusEntity = new BonusEntity();
            bonusEntity.setUserId(userId);
            bonusEntity.setType(1);
            bonusEntity.setDay(today);
            bonusEntity.setJobState(0);
            bonusEntity.setRoleName(userPart.getString("roleName"));
            bonusEntity.setBigRegionId(userPart.getString("regionId"));
            bonusEntity.setCompanyId(userPart.getString("companyId"));
            String roleName = userPart.getString("roleName");
            if ("区域经理".equals(roleName) || "区域组长".equals(roleName)) {
                String regionId = "";
                if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")) != null) {
                    regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")).getString("regionId");
                } else {
                    continue;
                }
                JSONObject num = getRegionDeviceType(userPart.getString("userId"), regionId);
                BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                BigDecimal targetNum = new BigDecimal(num.getString("targetNum"));
                Double money = 0.00;
                if (sumNum.subtract(targetNum).doubleValue() >= 0) {
                    money = 1000 * smallNum.doubleValue() + 2000 * middleNum.doubleValue() + 3000 * bigNum.doubleValue();
                    // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+sumNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSaleReward(new BigDecimal(money));
                    bonusEntity.setRemark(userPart.getString("departName") + "卖出" + sumNum + "台,应奖励" + money + "元");
                } else {
                    money = 1000 * (sumNum.subtract(targetNum)).doubleValue();
                    // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(targetNum.subtract(sumNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                    bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(sumNum)) + "台,应惩罚" + Math.abs(money) + "元");
                }
                bonusEntity.setSaleTargetNum(targetNum);
                bonusEntity.setSaleFinishNum(sumNum);
            } else if ("大区经理".equals(roleName)) {
                List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "AND t_sys_depart.ID='" + userPart.getString("departId") + "'");
                String regionId = "";
                if (bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")) != null) {
                    regionId = bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")).getString("id");
                    ;
                } else {
                    continue;
                }
                JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "'  AND MONTH='" + month + "' \n");
                BigDecimal targetNum = new BigDecimal(0);
                BigDecimal finishNum = new BigDecimal(0);
                BigDecimal finishsmallNum = new BigDecimal(0);
                BigDecimal finishmiddleNum = new BigDecimal(0);
                BigDecimal finishBigNum = new BigDecimal(0);
                Double money = 0.00;
                if (bigTarget != null) {
                    if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                        targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                    }
                }
                for (JSONObject region : regions) {
                    JSONObject num = getRegionFinish(region.getString("userId"), userPart.getString("regionId"), roleName, userPart.getString("userId"));
                    BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                    BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                    BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                    BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                    finishsmallNum = finishsmallNum.add(smallNum);
                    finishmiddleNum = finishmiddleNum.add(middleNum);
                    finishBigNum = finishBigNum.add(bigNum);
                    finishNum = finishNum.add(sumNum);
                }
                //大区经理自己的
                JSONObject self = getRegionFinish(userPart.getString("userId"), regionId, roleName, userPart.getString("userId"));
                finishsmallNum = finishsmallNum.add(new BigDecimal(self.getString("smallNum")));
                finishmiddleNum = finishmiddleNum.add(new BigDecimal(self.getString("middleNum")));
                finishBigNum = finishBigNum.add(new BigDecimal(self.getString("bigNum")));
                finishNum = finishNum.add(new BigDecimal(self.getString("sumNum")));
                if (finishNum.subtract(targetNum).doubleValue() >= 0) {
                    money = 1000 * finishsmallNum.doubleValue() + 2000 * finishmiddleNum.doubleValue() + 3000 * finishBigNum.doubleValue();
                    //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+finishNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSaleReward(new BigDecimal(money));
                    bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");
                } else {
                    money = 3000 * (finishNum.subtract(targetNum)).doubleValue();
                    // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(targetNum.subtract(finishNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                    bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                }
                bonusEntity.setSaleTargetNum(targetNum);
                bonusEntity.setSaleFinishNum(finishNum);
            } else {
                List<JSONObject> bigRegions = regionAchivementsDao.getBigRegionByUserId(userPart.getString("userId"), userPart.getString("companyId"));
                BigDecimal bigTargetSum = new BigDecimal(0);
                for (JSONObject bigRegion : bigRegions) {
                    BigDecimal targetNum = new BigDecimal(0);
                    String regionId = bigRegion.getString("regionId");
                    JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "'  AND MONTH='" + month + "'\n");
                    if (bigTarget != null) {
                        if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                            targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                        }
                    }
                    bigTargetSum = bigTargetSum.add(targetNum);
                }
                List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "");
                BigDecimal targetNum = new BigDecimal(0);
                BigDecimal finishNum = new BigDecimal(0);
                BigDecimal finishsmallNum = new BigDecimal(0);
                BigDecimal finishmiddleNum = new BigDecimal(0);
                BigDecimal finishBigNum = new BigDecimal(0);
                Double money = 0.00;
                for (JSONObject region : regions) {
                    String regionId = "";
                    if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")) != null) {
                        regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")).getString("regionId");
                    } else {
                        continue;
                    }
                    JSONObject num = getRegionFinish(region.getString("userId"), regionId, roleName, userPart.getString("userId"));
                    BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                    BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                    BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                    BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                    finishsmallNum = finishsmallNum.add(smallNum);
                    finishmiddleNum = finishmiddleNum.add(middleNum);
                    finishBigNum = finishBigNum.add(bigNum);
                    finishNum = finishNum.add(sumNum);
                }
                if (finishNum.subtract(bigTargetSum).doubleValue() >= 0) {
                    money = 150 * finishsmallNum.doubleValue() + 300 * finishmiddleNum.doubleValue() + 500 * finishBigNum.doubleValue();
                    //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+finishNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSaleReward(new BigDecimal(money));
                    bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");

                } else {
                    money = 500 * (finishNum.subtract(bigTargetSum)).doubleValue();
                    //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(bigTargetSum.subtract(finishNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                    bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                    bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (bigTargetSum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                }
                bonusEntity.setSaleTargetNum(bigTargetSum);
                bonusEntity.setSaleFinishNum(finishNum);
            }
            bonusDao.add(bonusEntity);
        }

        return "更新成功";
    }

    @Override
    public String deliveryCountMoney(String jsonData) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        String month = sdf1.format(new Date());
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String sampleId = jsonParams.getString("sampleId");
        JSONObject object = regionAchivementsDao.getRegionManagerId(sampleId);
        JSONObject sample = deviceSampleDao.getJSONById(sampleId);
        String acturalDate = "";
        if (sample != null) {
            acturalDate = sample.getString("actualDeliveryDate").substring(0, 7);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //防止不是当月数据更新，例如10.1  交付在途变已交，实际交车为9月。保存正确数据
        String today = acturalDate + "-30";
        if (object != null) {
            String regionManagerId = object.getString("regionManagerId");
            String companyId = object.getString("companyId");
            // 获取所有的兼职情况
            List<JSONObject> userParts = regionAchivementsDao.getUserListByRegionManagerId(companyId, regionManagerId);
            //删除从前的
            String ids = "";
            StringBuffer sql = new StringBuffer();
            if (userParts.size() > 0) {
                for (JSONObject jsonObject : userParts) {
                    sql.append("'");
                    sql.append(jsonObject.getString("userId"));
                    sql.append("'");
                    sql.append(",");
                }
                ids = sql.toString().substring(0, sql.toString().length() - 1);
            }
            bonusDao.removeByCondition(" AND TYPE=1 AND JOB_STATE=0 AND left(DAY,7) = '" + acturalDate + "' AND USER_ID IN(" + ids + ")", new String[]{});

            if (userParts.size() > 0) {
                for (JSONObject userPart : userParts) {
                    BonusEntity bonusEntity = new BonusEntity();
                    bonusEntity.setUserId(userPart.getString("userId"));
                    bonusEntity.setType(1);
                    bonusEntity.setJobState(0);
                    bonusEntity.setDay(today);
                    bonusEntity.setRoleName(userPart.getString("roleName"));
                    bonusEntity.setBigRegionId(userPart.getString("regionId"));
                    bonusEntity.setCompanyId(userPart.getString("companyId"));
                    String roleName = userPart.getString("roleName");
                    //if ("区域经理".equals(roleName)) {
                    if ("区域经理".equals(roleName) || "区域组长".equals(roleName)) {
                        String regionId = "";
                        if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")) != null) {
                            regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(userPart.getString("userId"), userPart.getString("departName")).getString("regionId");
                        } else {
                            continue;
                        }
                        JSONObject num = getRegionDeviceType(userPart.getString("userId"), regionId);
                        BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                        BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                        BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                        BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                        BigDecimal targetNum = new BigDecimal(num.getString("targetNum"));
                        Double money = 0.00;
                        if (sumNum.subtract(targetNum).doubleValue() >= 0) {
                            money = 1000 * smallNum.doubleValue() + 2000 * middleNum.doubleValue() + 3000 * bigNum.doubleValue();
                            // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+sumNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSaleReward(new BigDecimal(money));
                            bonusEntity.setRemark(userPart.getString("departName") + "卖出" + sumNum + "台,应奖励" + money + "元");
                        } else {
                            money = 1000 * (sumNum.subtract(targetNum)).doubleValue();
                            // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(targetNum.subtract(sumNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                            bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(sumNum)) + "台,应惩罚" + Math.abs(money) + "元");
                        }
                        bonusEntity.setSaleTargetNum(targetNum);
                        bonusEntity.setSaleFinishNum(sumNum);
                        //} else if ("大区经理".equals(roleName)||"区域组长".equals(roleName)) {
                    } else if ("大区经理".equals(roleName)) {
                        List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "AND t_sys_depart.ID='" + userPart.getString("departId") + "'");
                        String regionId = "";
                        if (bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")) != null) {
                            regionId = bigAchievementsDao.getBigRegionTargetBycompanyId(userPart.getString("departName"), userPart.getString("companyId")).getString("id");
                            ;
                        } else {
                            continue;
                        }
                        JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "' AND MONTH='" + month + "'\n");
                        BigDecimal targetNum = new BigDecimal(0);
                        BigDecimal finishNum = new BigDecimal(0);
                        BigDecimal finishsmallNum = new BigDecimal(0);
                        BigDecimal finishmiddleNum = new BigDecimal(0);
                        BigDecimal finishBigNum = new BigDecimal(0);
                        Double money = 0.00;
                        if (bigTarget != null) {
                            if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                            }
                        }
                        for (JSONObject region : regions) {
                            JSONObject num = getRegionFinish(region.getString("userId"), userPart.getString("regionId"), roleName, userPart.getString("userId"));
                            BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                            BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                            BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                            BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                            finishsmallNum = finishsmallNum.add(smallNum);
                            finishmiddleNum = finishmiddleNum.add(middleNum);
                            finishBigNum = finishBigNum.add(bigNum);
                            finishNum = finishNum.add(sumNum);
                        }
                        //大区经理自己的
                        JSONObject self = getRegionFinish(userPart.getString("userId"), regionId, roleName, userPart.getString("userId"));
                        finishsmallNum = finishsmallNum.add(new BigDecimal(self.getString("smallNum")));
                        finishmiddleNum = finishmiddleNum.add(new BigDecimal(self.getString("middleNum")));
                        finishBigNum = finishBigNum.add(new BigDecimal(self.getString("bigNum")));
                        finishNum = finishNum.add(new BigDecimal(self.getString("sumNum")));
                        if (finishNum.subtract(targetNum).doubleValue() >= 0) {
                            money = 1000 * finishsmallNum.doubleValue() + 2000 * finishmiddleNum.doubleValue() + 3000 * finishBigNum.doubleValue();
                            //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+finishNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSaleReward(new BigDecimal(money));
                            bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");
                        } else {
                            money = 3000 * (finishNum.subtract(targetNum)).doubleValue();
                            // newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(targetNum.subtract(finishNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                            bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (targetNum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                        }
                        bonusEntity.setSaleTargetNum(targetNum);
                        bonusEntity.setSaleFinishNum(finishNum);
                    } else {
                        List<JSONObject> bigRegions = regionAchivementsDao.getBigRegionByUserId(userPart.getString("userId"), userPart.getString("companyId"));
                        BigDecimal bigTargetSum = new BigDecimal(0);
                        for (JSONObject bigRegion : bigRegions) {
                            BigDecimal targetNum = new BigDecimal(0);
                            String regionId = bigRegion.getString("regionId");
                            JSONObject bigTarget = bigAchievementsDao.getListByBigAreaId("AND t_sys_big_achievements.BIG_AREA_ID= '" + regionId + "' AND MONTH='" + month + "' \n");
                            if (bigTarget != null) {
                                if (!StringUtils.isBlank(bigTarget.getString("saleTargetNum"))) {
                                    targetNum = new BigDecimal(bigTarget.getString("saleTargetNum"));
                                }
                            }
                            bigTargetSum = bigTargetSum.add(targetNum);
                        }
                        List<JSONObject> regions = regionAchivementsDao.getBigRegionList(userPart.getString("userId"), userPart.getString("companyId"), "'区域经理','区域组长'", "");
                        BigDecimal targetNum = new BigDecimal(0);
                        BigDecimal finishNum = new BigDecimal(0);
                        BigDecimal finishsmallNum = new BigDecimal(0);
                        BigDecimal finishmiddleNum = new BigDecimal(0);
                        BigDecimal finishBigNum = new BigDecimal(0);
                        Double money = 0.00;
                        for (JSONObject region : regions) {
                            String regionId = "";
                            if (regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")) != null) {
                                regionId = regionAchivementsDao.getBigRegionIdByUserIdAndCompanyId(region.getString("userId"), region.getString("departName")).getString("regionId");
                            } else {
                                continue;
                            }
                            JSONObject num = getRegionFinish(region.getString("userId"), regionId, roleName, userPart.getString("userId"));
                            BigDecimal smallNum = new BigDecimal(num.getString("smallNum"));
                            BigDecimal middleNum = new BigDecimal(num.getString("middleNum"));
                            BigDecimal bigNum = new BigDecimal(num.getString("bigNum"));
                            BigDecimal sumNum = new BigDecimal(num.getString("sumNum"));
                            finishsmallNum = finishsmallNum.add(smallNum);
                            finishmiddleNum = finishmiddleNum.add(middleNum);
                            finishBigNum = finishBigNum.add(bigNum);
                            finishNum = finishNum.add(sumNum);
                        }
                        if (finishNum.subtract(bigTargetSum).doubleValue() >= 0) {
                            money = 150 * finishsmallNum.doubleValue() + 300 * finishmiddleNum.doubleValue() + 500 * finishBigNum.doubleValue();
                            //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"卖出"+finishNum+"台,应奖励"+money+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSaleReward(new BigDecimal(money));
                            bonusEntity.setRemark(userPart.getString("departName") + "卖出" + finishNum + "台,应奖励" + money + "元");

                        } else {
                            money = 500 * (finishNum.subtract(bigTargetSum)).doubleValue();
                            //newJsonoObject=compareTo(jlJsonObject,cfJsonObject,String.valueOf(money),userPart.getString("departName")+"未完成"+(bigTargetSum.subtract(finishNum))+"台,应惩罚"+Math.abs(money)+"元",roleName,userPart.getString("regionId"),userPart.getString("companyId"));
                            bonusEntity.setSalePunishment(new BigDecimal(Math.abs(money)));
                            bonusEntity.setRemark(userPart.getString("departName") + "未完成" + (bigTargetSum.subtract(finishNum)) + "台,应惩罚" + Math.abs(money) + "元");
                        }
                        bonusEntity.setSaleTargetNum(bigTargetSum);
                        bonusEntity.setSaleFinishNum(finishNum);
                    }
                    bonusDao.add(bonusEntity);
                }
            }


        }
        return "更新成功";
    }

    public JSONObject compareTo(JSONObject jlJsonObject, JSONObject cfJsonObject, String money, String remark, String roleName, String regionId, String companyId) {
        BigDecimal newMoney = new BigDecimal(money);
        JSONObject newJsonoObject = null;
        if (newMoney.doubleValue() >= 0) {
            newJsonoObject = jlJsonObject;
        } else {
            newJsonoObject = cfJsonObject;
        }
        BigDecimal oldMoney = new BigDecimal(newJsonoObject.getString("money"));
        if (Math.abs(oldMoney.doubleValue()) < Math.abs(newMoney.doubleValue())) {
            newJsonoObject.put("money", newMoney.toString());
            newJsonoObject.put("roleName", roleName);
            newJsonoObject.put("regionId", regionId);
            newJsonoObject.put("companyId", companyId);
        }
        StringBuffer sb = new StringBuffer();
        sb.append(newJsonoObject.getString("remark"));
        if (!StringUtils.isBlank(newJsonoObject.getString("remark"))) {
            sb.append(",");
        }
        sb.append(remark);
        newJsonoObject.put("remark", sb.toString());
        if (newJsonoObject.getString("money") == null || newJsonoObject == null) {
            System.out.println("1111" + remark);
        }
        return newJsonoObject;

    }

    public JSONObject getRegionDeviceType(String userId, String regionId) {
        List<JSONObject> devices = new ArrayList<>();
        JSONObject target = regionAchivementsDao.isSale(userId, regionId);
        if (target != null) {
            devices = regionAchivementsDao.getRegionDeviceType(userId, target);
        }
        JSONObject num = new JSONObject();
        int smallNum = 0;
        int middleNum = 0;
        int bigNum = 0;
        int sumNum = 0;
        int targetNum = 0;
        JSONObject targetObject = regionAchivementsDao.getTargetByUserId(userId, regionId);
        if (targetObject != null) {
            targetNum = Integer.parseInt(targetObject.getString("targetNum"));
        }
        if (devices.size() > 0) {
            for (JSONObject device : devices) {
                String ton = device.getString("tonConfig");
                Double tonConfig = 0.00;
                if (!StringUtils.isBlank(ton)) {
                    tonConfig = Double.parseDouble(ton);
                    if (tonConfig <= 10) {
                        smallNum = smallNum + 1;
                    } else if (tonConfig > 10 && tonConfig <= 30) {
                        middleNum = middleNum + 1;
                    } else {
                        bigNum = bigNum + 1;
                    }
                }
                sumNum = smallNum + middleNum + bigNum;

            }
        }
        num.put("smallNum", smallNum);
        num.put("middleNum", middleNum);
        num.put("bigNum", bigNum);
        num.put("sumNum", sumNum);
        num.put("targetNum", targetNum);
        return num;
    }

    public JSONObject getRegionFinish(String userId, String regionId, String roleName, String parentId) {
        String parentName = "";
        switch (roleName) {
            case "大区经理":
                parentName = "BIG_REGION_MANAGER_ID";
                break;
            case "区域总监":
                parentName = "REGION_DIRECTOR_ID";
                break;
            case "销售总监":
                parentName = "DIRECTOR_OF_SALES_ID";
                break;
            case "总经理":
                parentName = "GENERAL_MANAGER_ID";
                break;
        }
        List<JSONObject> devices = regionAchivementsDao.getDeviceType(userId, parentId, parentName);
        JSONObject num = new JSONObject();
        int smallNum = 0;
        int middleNum = 0;
        int bigNum = 0;
        int sumNum = 0;
        int targetNum = 0;
        JSONObject targetObject = regionAchivementsDao.getTargetByUserId(userId, regionId);
        if (targetObject != null) {
            targetNum = Integer.parseInt(targetObject.getString("targetNum"));
        }
        if (devices.size() > 0) {
            for (JSONObject device : devices) {
                String ton = device.getString("tonConfig");
                Double tonConfig = 0.00;
                if (!StringUtils.isBlank(ton)) {
                    tonConfig = Double.parseDouble(ton);
                    if (tonConfig <= 10) {
                        smallNum = smallNum + 1;
                    } else if (tonConfig > 10 && tonConfig <= 30) {
                        middleNum = middleNum + 1;
                    } else {
                        bigNum = bigNum + 1;
                    }
                }
                sumNum = smallNum + middleNum + bigNum;

            }
        }
        num.put("smallNum", smallNum);
        num.put("middleNum", middleNum);
        num.put("bigNum", bigNum);
        num.put("sumNum", sumNum);
        num.put("targetNum", targetNum);
        return num;
    }

    public void removeCurrentData() {
        bonusDao.removeByCondition(" AND TYPE=1 AND DAY = LEFT (NOW(), 7)", new String[]{});
    }

    public JSONObject getListByBigAreaId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String bigAreaId = jsonParams.getString("bigAreaId");
        StringBuffer condition = new StringBuffer();
        condition.append("AND t_sys_big_achievements.BIG_AREA_ID= '" + bigAreaId + "'\n");
        List<Object> sqlArgs = new ArrayList<Object>();
        JSONObject records = bigAchievementsDao.getListByBigAreaId(condition.toString());
        // 子数组对象
        JSONObject result = new JSONObject();
        result.put("records", records);
        return result;
    }
}
